#pragma once
#include <xpam/serialization/serialize.h>
#include <xpam/serialization/naming.h>
#include <xpam/lexical_cast.h>
#include <string>
#include <vector>
namespace xpam
{
    namespace serialization
    {
        class ostorage_txt
        {
            std::ostream & stream;
            std::vector<std::string> names;
            std::string generate_name()
            {
                std::string s;
                for (int i = 0; i < (int)names.size(); i++)
                    s += "." + names[i];
                return s;
            }
        public:
            ostorage_txt(std::ostream & s) : stream(s)
            { }

            void push_name(const std::string & n)
            {
                names.push_back(n);
            }

            void pop_name()
            {
                names.resize(names.size() - 1);
            }

            template<class T>
            void write(T & t)
            {
                stream << generate_name() << "=" << t << std::endl;
            }
        };

        inline void serialize(ostorage_txt & s, bool & t)
        {
            s.write(t);
        }

        inline void serialize(ostorage_txt & s, int & t)
        {
            s.write(t);
        }

        inline void serialize(ostorage_txt & s, std::string & t)
        {
            s.write(t);
        }

        template<class T>
        void serialize(ostorage_txt & s, std::vector<T> & t)
        {
            int n = t.size();
            serialize(s, naming(n, "count"));
            for (int i = 0; i < n; i++)
                serialize(s, naming(t[i], "item[" + lexical_cast<std::string>(i) + "]"));
        }
    }
}